package com.mef.smsplat.web.controller.sys.user;

import java.util.Date;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hc24.util.IDUtil;
import com.hc24.util.StringUtil;
import com.mef.smsplat.dao.base.DataGridModel;
import com.mef.smsplat.dao.base.Page;
import com.mef.smsplat.domain.SysAdminInfoDomain;
import com.mef.smsplat.exception.BusinessException;
import com.mef.smsplat.service.SysAdminInfoService;
import com.mef.smsplat.util.Constants;
import com.mef.smsplat.web.controller.base.Json;

@Controller
@RequestMapping("/sys/sysadmin")
public class SysAdminInfoController {
	private SysAdminInfoService sysAdminInfoService;

	public void setSysAdminInfoService(SysAdminInfoService sysAdminInfoService) {
		this.sysAdminInfoService = sysAdminInfoService;
	}
	
	@ResponseBody
	@RequestMapping("/login.sys")
	public Json login(HttpSession session,String adminName,String adminPassword){
		if(adminName==null||"".equals(adminName))
			throw new BusinessException("用户名不能为空!");
		if(adminPassword==null||"".equals(adminPassword))
			throw new BusinessException("密码不能为空!");
		SysAdminInfoDomain admin= sysAdminInfoService.login(adminName, adminPassword);
		if(admin!=null){
			session.setAttribute(Constants.SESSION_TYPE, Constants.SESSION_SYS_ADMIN);
			session.setAttribute(Constants.SESSION_ADMIN, admin);
			Json json=new Json();
			json.setData(admin);
			return json;
			
		}else{
			Json json=new Json();
			json.setSuccess(false);
			json.setMsg("用户名或者密码错误!");
			return json;
		}
	}
	
	@ResponseBody
	@RequestMapping("/query.sys")
	public Page<SysAdminInfoDomain> query(String adminName,DataGridModel dgm){
		return sysAdminInfoService.query(adminName, dgm);
	}
	
	@ResponseBody
	@RequestMapping("/add.sys")
	public Json add(SysAdminInfoDomain admin){
		if(admin.getAdminName()==null || "".equals(admin.getAdminName().trim())){
			throw new BusinessException("用户名不能为空");
		}
		if(admin.getTrueName()==null || "".equals(admin.getTrueName().trim())){
			throw new BusinessException("真实姓名不能为空");
		}
		if(!sysAdminInfoService.isUnique(admin.getAdminName(), admin.getSysAdminId())){
			throw new BusinessException("此账号已经存在");
		}
		admin.setSysAdminId(IDUtil.next()+"");
		admin.setCreateTime(new Date());
		admin.setAdminPassword(StringUtil.md5(Constants.PASSWORD));//默认密码
		sysAdminInfoService.save(admin);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/update.sys")
	public Json update(SysAdminInfoDomain admin){
		if(admin.getAdminName()==null || "".equals(admin.getAdminName().trim())){
			throw new BusinessException("用户名不能为空");
		}
		if(admin.getTrueName()==null || "".equals(admin.getTrueName().trim())){
			throw new BusinessException("真实姓名不能为空");
		}
		if(!sysAdminInfoService.isUnique(admin.getAdminName(), admin.getSysAdminId())){
			throw new BusinessException("此账号已经存在");
		}
		sysAdminInfoService.update(admin);
		return new Json();
	}
	
	@ResponseBody
	@RequestMapping("/del.sys")
	public Json del(String[] ids){
		sysAdminInfoService.removeById(ids);
		return new Json();
	}
	
	
}
